/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package psc;

import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.general.Series;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Taeko
 */
public class Principal {

    static double periodo;
    static double amplitude;
    static double offset;
    static double tempo;
    static double ams;
    static double saida;
    static XYSeriesCollection datasetXY;
    static sinais ondas;
    /**
     * @param args the command line arguments
     */
static XYSeries xys = new XYSeries("amostras");
static TimeSeries ts = new TimeSeries("Sinal");

    public static void main(String[] args) throws InterruptedException {
        
        periodo = 5;
        amplitude = 5;
        offset = 0.1;
        tempo = 0;
        ams=0;
        
        ondas = new sinais(periodo, amplitude, offset);
        
        gen myGen = new gen();
        new Thread(myGen).start();

       datasetXY = new XYSeriesCollection(xys);
//       JFreeChart chart = ChartFactory.createXYLineChart(
//            "Teste", 
//            "Amplitude", 
//            "Tempo",
//            datasetXY, 
//            PlotOrientation.HORIZONTAL, 
//            true, 
//            true, 
//            false);
      TimeSeriesCollection dataset = new TimeSeriesCollection(ts);
      JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Onda Quadrada",
            "Tempo",
            "Amplitude",
            dataset,
            true,
            true,
            false
        );
        final XYPlot plot = chart.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);

        JFrame frame = new JFrame("GraphTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ChartPanel label = new ChartPanel(chart);
        frame.getContentPane().add(label);
        //Suppose I add combo boxes and buttons here later

        frame.pack();
        frame.setVisible(true);
    }

    static class gen implements Runnable {

        @Override
        public void run() {
            while(true) {
                
                saida = ondas.ondaQuadrada();

                System.out.println(saida);
                //xys.addOrUpdate(saida,ams);
                ts.addOrUpdate(new Millisecond(), saida);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    System.out.println(ex);
                }
            }
        }
    }

}
